package com.study.crud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.crud.model.emp.Emp;
import com.study.crud.model.job.Job;
import com.study.crud.model.user.Login;
import com.study.crud.model.user.User;
import com.study.crud.service.EmpService;
import com.study.crud.service.JobService;
import com.study.crud.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private JobService jobService;

    @Resource
    private EmpService empService;

    /**
     * 通过登录的eid查询权限，来进行查找并返回对应的展示页面
     * */
    @RequestMapping("/Choose")
    public String Choose(Integer userId,Model model,HttpSession session){
        //创建一个空的userDto
        User u = new User();

        //判断 jid==4  true   只能查看本部门
        User user = userService.getById(userId);
        //在login得到数据
        Login login = new Login();
        login.setLoginId(user.getUid());
        login.setLoginName(user.getUname());
        login.setJid(user.getJid());

        //存入session域
        session.setAttribute("login",login);

        //判断是否为4
        if (user.getJid()==4){
            //return "redirect:/user/findList";
            //return this.findList(uUser,user,model);
            return this.findList(u,model,session);
        }else {
            return this.findAll(u,model,session);
        }
    }

    /**
     * 查看本部门员工信息
     * 不能 增 删 改
     * 可以 查
     * */
    @RequestMapping("/findList")
    public String findList(User user,Model model,HttpSession session){

        //将登录后携带的用户数据添加进model
        //添加搜索的数据 进入model
        model.addAttribute("user",user);
        //添加login
        //添加session
        //model.addAttribute("login",login);


        //得到 emp部门表 里的数据
        List<Emp> empList = empService.list(new QueryWrapper<>());
        model.addAttribute("empList",empList);
        //得到 job职务表 里的数据
        List<Job> jobList = jobService.list(new QueryWrapper<>());
        model.addAttribute("jobList",jobList);

        List<User> userList = userService.findListBy4(user.getUname());//只能查找 本部门 的员工
        model.addAttribute("userList",userList);
        return "user/userList1";
    }

    /**
     * jid！= 4 --->可以查看全部员工信息
     * */
    @RequestMapping("/findAll")
    public String findAll(User user, Model model, HttpSession session) {


        //将登录后携带的用户数据添加进model
        //添加搜索的数据 进入model
        model.addAttribute("user",user);

        //添加login
        //存入session域
        //session.setAttribute("login",login);

        //得到 emp部门表 里的数据
        List<Emp> empList = empService.list(new QueryWrapper<>());
        model.addAttribute("empList",empList);
        //得到 job职务表 里的数据
        List<Job> jobList = jobService.list(new QueryWrapper<>());
        model.addAttribute("jobList",jobList);

        //查找全部员工信息
        List<User> userList = userService.findAll(user);
        model.addAttribute("userList",userList);
        return "user/userList";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        //得到 job职务表 里的数据
        List<Job> jobList = jobService.list(new QueryWrapper<>());
        model.addAttribute("jobList",jobList);
        return "user/addUser";
    }

    /**
     * 添加
     * */
    @RequestMapping("/insert")
    public String insert(User user) {
       //通过user.getJid找到对应部门的eid
        user.setEid(jobService.findEidByJid(user.getJid()));
        //添加的默认密码都是 123456
        user.setPwd(123456);
        //添加数据
        userService.save(user);
        //redirectAttributes.addAttribute("user",user);
        return "redirect:/user/findAll";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer uid,Model model){
        //得到 job职务表 里的数据
        List<Job> jobList = jobService.list(new QueryWrapper<>());
        model.addAttribute("jobList",jobList);
        //得到对应uid的内容
        User user = userService.getById(uid);
        model.addAttribute("user",user);
        return "user/updateUser";
    }

    /**
     * 修改
     * */
    @RequestMapping("/update")
    public String update(User user) {
        userService.updateById(user);
        //redirectAttributes.addAttribute("user",user);
        return "redirect:/user/findAll";
    }

    @RequestMapping("/deleteByUid")
    public String deleteByUid(Integer uid){
        userService.removeById(uid);
        return "redirect:/user/findAll";
    }

}
